/**
 * 读取目录下所有文件的内容
 */
import common from '../../tnxet-common.js';
import util from '../main-util.js';
import fs from 'fs';

export default {
    name: common.event.file.readAll,
    listen(dir, pathRegex) {
        return new Promise((resolve, reject) => {
            if (dir) {
                dir = util.file.getAbsolutePathBasedOnApp(dir);
                let all = {};
                if (util.file.loopDir(dir, pathRegex, filePath => {
                    let buffer = fs.readFileSync(filePath);
                    all[filePath] = buffer.toString();
                })) {
                    resolve(all);
                } else {
                    reject(dir + ' is not a directory');
                }
            } else {
                reject('dir is ' + dir);
            }
        });
    },
}
